var express = require('express');
var router = express.Router();

var JPush = require("../lib/JPush.js");

var address = {
	"passenger":{
		"lat":'31',
		"lon":'118'
	},
	"driver":{
		"lat":'31.1',
		"lon":'118.1'
	}
};

var pushTerminal = function(appkey, masterSecret, message, type){
	var client = JPush.buildClient(appkey, masterSecret);

	//easy push
	client.push().setPlatform(JPush.ALL)
	    .setAudience(JPush.alias(type))
	    .setNotification(message, JPush.android('android alert', null, 1))
	    .send(function(err, res) {
	        if (err) {
	            console.log(err.message);
	        } else {
	            console.log('Sendno: ' + res.sendno);
	            console.log('Msg_id: ' + res.msg_id);
	        }
	    });
}

//passenger and driver's address
router.get('/toforeground', function(req, res){
	res.jsonp(address);
});

router.post('/postpassenger', function(req, res){
	var lat = req.body.lat;
	var lon = req.body.lon
	address.passenger.lat = lat;
	address.passenger.lon = lon;
	res.jsonp({status:'ok'});
});

router.post('/postdriver', function(req, res){
	var lat = req.body.lat;
	var lon = req.body.lon
	address.driver.lat = lat;
	address.driver.lon = lon;
	res.jsonp({status:'ok'});
});

router.get('/getpassenger', function(req, res){
	res.jsonp(address.passenger);
});

router.get('/getdriver', function(req, res){
	res.jsonp(address.driver);
});

router.get('/passengercall', function(req, res){
	pushTerminal('8fe07689d84f644ac617973b', '8bedebfc1a7e50b9b79cfcac', 'passenger call', 'driver');
	res.jsonp({status:'ok'});
});

router.get('/driveranswer', function(req, res){
	pushTerminal('da542abb1dfae4ad09104f6b', '332043472c2c2f0cea927e7d', 'driver answer', 'user');
	res.jsonp({status:'ok'});
});

module.exports = router;


// var a2 = arr.find(function (obj) {
//             return obj.id > 1;
//         });




